/*
 ============================================================================
 Name		: ImageUtils.cpp
 Author	  : Lenka Litvova
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CImageUtils implementation
 ============================================================================
 */

#include "ImageUtils.h"
#include "RMask.h"
#include <ncvMedianFilterOp.h> //Median filter from NokiaCV
#include <ncvEdgeDetectionOp.h> //Sobel filter
#include <e32math.h>

CImageUtils::CImageUtils()
	{
	// No implementation required
	}

CImageUtils::~CImageUtils()
	{
	}

CImageUtils* CImageUtils::NewLC()
	{
	CImageUtils* self = new (ELeave) CImageUtils();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CImageUtils* CImageUtils::NewL()
	{
	CImageUtils* self = CImageUtils::NewLC();
	CleanupStack::Pop(); // self;
	return self;
	}

void CImageUtils::ConstructL()
	{

	}

CFbsBitmap* CImageUtils::CopyImageL(CFbsBitmap *aImage)
	{
		CFbsBitmap *copy;
		TSize s = aImage->SizeInPixels();
	    User::LeaveIfError(copy->Create(s, aImage->DisplayMode()));
	   
	    TBitmapUtil orig_util(aImage);
	    TBitmapUtil dest_util(copy);
	    orig_util.Begin(TPoint(0,0));
	    dest_util.Begin(TPoint(0,0), orig_util);
	   
	    TInt xPos;
	    for (TInt yPos=0;yPos<s.iHeight;yPos++)
	        {
	        orig_util.SetPos(TPoint(0,yPos));
	        dest_util.SetPos(TPoint(0,yPos));
	        for (xPos=0;xPos<s.iWidth;xPos++)
	            {
	            dest_util.SetPixel(orig_util);
	            orig_util.IncXPos();
	            dest_util.IncXPos();
	            }
	        }
	    orig_util.End();
	    dest_util.End();
	    
	    return copy;
	}

CNokiaCVImage* CImageUtils::CopyImageCVL(CNokiaCVImage *aImage)
	{
		CNokiaCVImage *copy = CNokiaCVImage::NewL();
		CleanupStack::PushL(copy);
		TSize size = aImage->Size();
	    copy->CreateL(size, aImage->DisplayMode());
	   
	    CPixelAccess *pa = CPixelAccess::NewLC(copy);
	    CleanupStack::PushL(pa);
	   
	    TInt xPos;
	    for (TInt yPos=0;yPos<size.iHeight;yPos++)
	        {
				for (xPos=0;xPos<size.iWidth;xPos++)
	            {
	            TPoint p = TPoint(xPos, yPos);
	            pa->SetPos(p);
	            TRgb c;
	            aImage->GetPixel(c, p);
	            pa->SetRGB(c.Value());
	            }
	        }
	    
	    CleanupStack::PopAndDestroy(pa);
	    CleanupStack::Pop();
	    return copy;
	}

TBool CImageUtils::IsFaceColorRGB(TRgb aColor)
	{
		TInt red = aColor.Red();
		TInt green = aColor.Green();
		TInt blue = aColor.Blue();
		
	//	RDebug::Print(_L("Red:%d Green:%d Blue:%d"), red, green, blue);

	    if(
	            (	red > 95 && green > 40 && blue > 20 &&
	            	(Max(red, Max(blue, green)) - Min(red, Min(blue, green))) > 15 &&
	            	Abs(red-green)>15 &&
	            	red > green &&
	            	red > blue) ||
	            (	red > 220 && green > 210 && blue > 170 &&
	                Abs(red-green)<=15 &&
	                red > blue &&
	                green > blue
	            )
	       )
	             return ETrue;
	         else
	             return EFalse;
	}

void CImageUtils::ConvertFaceColorWhite(CNokiaCVImage *aImage, CArea *aFace)
	{
		CPixelAccess *pa = CPixelAccess::NewLC(aImage);
		TSize s = aImage->Size();
		
		TUint32 black = TRgb(0, 0, 0).Value();
		TUint32 white = TRgb(255, 255, 255).Value();
		
		for(TInt row=aFace->GetTop().iY; row< aFace->GetBottom().iY+1;row++)
			for(TInt col=aFace->GetLeft().iX;col<aFace->GetRight().iX;col++)
			{
				TPoint p = TPoint(col, row);
				pa->SetPos(p);
				TRgb c;
				aImage->GetPixel(c, p);
				
				if(IsFaceColorRGB(c))
					pa->SetRGB(white);
				else
					pa->SetRGB(black);
			}
		
		CleanupStack::PopAndDestroy(pa);
	}

CArea* CImageUtils::FillFaceAreaL(CFbsBitmap* aImage, TPoint aBegin)
	{
		CFbsBitmap* img = CopyImageL(aImage);
		CleanupStack::PushL(img);
		TBitmapUtil util(img);
		TSize s = aImage->SizeInPixels();
	    CArea *a = CArea::NewL(aBegin, s.iHeight);

	    TUint32 newc = TRgb(255, 255, 255).Value();

	    CArrayFixFlat<TPoint> *q = new CArrayFixFlat<TPoint>(300);
	    q->AppendL(aBegin);

	    util.Begin(aBegin);
	    while(q->Count() != 0)
	    {
			TPoint p = q->At(0); 
			q->Delete(0);
	        TInt x = p.iX;
	        TInt y = p.iY;

	        if(x<0 || y<0 || x >= s.iWidth || y>=s.iHeight)
	        	continue;
	        
	        util.SetPos(p);
	        TRgb c = util.GetPixel();

	        if(
	                    IsFaceColorRGB(c) &&
	                    c.Value() != newc
	          )
	        {
	                util.SetPixel(newc);
	                a->Update(p);
	                q->AppendL(TPoint(x, y-1));
	                q->AppendL(TPoint(x+1, y-1));
	                q->AppendL(TPoint(x+1, y));
	                q->AppendL(TPoint(x+1, y+1));
	                q->AppendL(TPoint(x, y+1));
	                q->AppendL(TPoint(x-1, y+1));
	                q->AppendL(TPoint(x-1, y));
	                q->AppendL(TPoint(x-1, y-1));
	        }
	    }
	    util.End();
	    CleanupStack::PopAndDestroy(img);
	    delete q;
	    return a;
	}

CArea* CImageUtils::FillFaceAreaCVL(CNokiaCVImage* aImage, TPoint aBegin)
	{
		CPixelAccess *pa = CPixelAccess::NewLC(aImage);
		TSize s = aImage->Size();
	    CArea *a = CArea::NewL(aBegin, s.iHeight);

	    TUint32 newc = TRgb(255, 255, 255).Value();
	    TUint32 black = TRgb(0,0, 0).Value();
	    
	    CArrayFixFlat<TPoint> *q = new CArrayFixFlat<TPoint>(300);
	    q->AppendL(aBegin);

	    while(q->Count() != 0)
	    {
			TPoint p = q->At(0); 
			q->Delete(0);
	        TInt x = p.iX;
	        TInt y = p.iY;

	        if(x<0 || y<0 || x >= s.iWidth || y>=s.iHeight)
	        	continue;
	       
	        pa->SetPos(p);
	        TRgb c;
	        aImage->GetPixel(c, p);

	        if(
	                    IsFaceColorRGB(c) &&
	                    c.Value() != newc
	          )
	        {
	                pa->SetRGB(newc);
	                a->Update(p);
	                q->AppendL(TPoint(x, y-1));
	                q->AppendL(TPoint(x+1, y-1));
	                q->AppendL(TPoint(x+1, y));
	                q->AppendL(TPoint(x+1, y+1));
	                q->AppendL(TPoint(x, y+1));
	                q->AppendL(TPoint(x-1, y+1));
	                q->AppendL(TPoint(x-1, y));
	                q->AppendL(TPoint(x-1, y-1));
	        }
	    }
	    CleanupStack::PopAndDestroy(pa);
	    delete q;
	    return a;
	}

CArea* CImageUtils::FillNonFaceAreaL(CFbsBitmap *aImage, TPoint aBegin, CArea *face)
	{
			CFbsBitmap* img = CopyImageL(aImage);
			CleanupStack::PushL(img);
			TBitmapUtil util(img);
			TSize s = aImage->SizeInPixels();
		    CArea *a = CArea::NewL(aBegin, s.iHeight);

		    TUint32 newc = TRgb(255, 255, 255).Value();

		    CArrayFixFlat<TPoint> *q = new CArrayFixFlat<TPoint>(300);
		    q->AppendL(aBegin);

		    util.Begin(aBegin);
		    while(q->Count() != 0)
	        {
				TPoint p = q->At(0); 
	        	q->Delete(0);
	        	TInt x = p.iX;
	        	TInt y = p.iY;

	            if(y<face->GetTop().iY || y>face->GetBottom().iY || x<face->GetLeftBorderL(y).iX || x > face->GetRightBorderL(y).iX)
	                continue;

	            util.SetPos(p);
	            TRgb c = util.GetPixel();

	            if(
	            	  IsFaceColorRGB(c) == EFalse &&
	            	  c.Value() != newc
	              )
	            {
					util.SetPixel(newc);
	            	a->Update(p);
	            	q->AppendL(TPoint(x, y-1));
	            	q->AppendL(TPoint(x+1, y-1));
	            	q->AppendL(TPoint(x+1, y));
	            	q->AppendL(TPoint(x+1, y+1));
	            	q->AppendL(TPoint(x, y+1));
	            	q->AppendL(TPoint(x-1, y+1));
	            	q->AppendL(TPoint(x-1, y));
	            	q->AppendL(TPoint(x-1, y-1));
	            }
	        }
		    util.End();
		    CleanupStack::PopAndDestroy(img);
		    delete q;
	        return a;
	}

CArea* CImageUtils::FillNonFaceAreaCVL(CNokiaCVImage *aImage, TPoint aBegin, CArea *face)
	{
			CPixelAccess *pa = CPixelAccess::NewLC(aImage);
			TSize s = aImage->Size();
		    CArea *a = CArea::NewL(aBegin, s.iHeight);

		    TUint32 newc = TRgb(255, 255, 255).Value();

		    CArrayFixFlat<TPoint> *q = new CArrayFixFlat<TPoint>(300);
		    q->AppendL(aBegin);

		    while(q->Count() != 0)
	        {
				TPoint p = q->At(0); 
	        	q->Delete(0);
	        	TInt x = p.iX;
	        	TInt y = p.iY;

	            if(y<face->GetTop().iY || y>face->GetBottom().iY || x<face->GetLeftBorderL(y).iX || x > face->GetRightBorderL(y).iX)
	                continue;

	            pa->SetPos(p);
	            TRgb c;
	            aImage->GetPixel(c, p);

	            if(
	            	  IsFaceColorRGB(c) == EFalse &&
	            	  c.Value() != newc
	              )
	            {
					pa->SetRGB(newc);
	            	a->Update(p);
	            	q->AppendL(TPoint(x, y-1));
	            	q->AppendL(TPoint(x+1, y-1));
	            	q->AppendL(TPoint(x+1, y));
	            	q->AppendL(TPoint(x+1, y+1));
	            	q->AppendL(TPoint(x, y+1));
	            	q->AppendL(TPoint(x-1, y+1));
	            	q->AppendL(TPoint(x-1, y));
	            	q->AppendL(TPoint(x-1, y-1));
	            }
	        }		   
		    CleanupStack::PopAndDestroy(pa);
		    delete q;
	        return a;
	}

CArea* CImageUtils::FillNonFaceAreaBWCVL(CNokiaCVImage *aImage, TPoint aBegin, CArea *face)
	{
			CPixelAccess *pa = CPixelAccess::NewLC(aImage);
			TSize s = aImage->Size();
		    CArea *a = CArea::NewL(aBegin, s.iHeight);

		    TUint32 newc = TRgb(200, 200, 200).Value();
		    TUint32 black = TRgb(0, 0, 0).Value();
		    TUint32 white = TRgb(255, 255, 255).Value();

		    CArrayFixFlat<TPoint> *q = new CArrayFixFlat<TPoint>(300);
		    q->AppendL(aBegin);

		    while(q->Count() != 0)
	        {
				TPoint p = q->At(0); 
	        	q->Delete(0);
	        	TInt x = p.iX;
	        	TInt y = p.iY;

	            if(y<face->GetTop().iY || y>face->GetBottom().iY || x<face->GetLeftBorderL(y).iX || x > face->GetRightBorderL(y).iX)
	                continue;

	            pa->SetPos(p);
	            TRgb c;
	            aImage->GetPixel(c, p);

	            if(
	            	  c.Value() == black &&
	            	  c.Value() != newc
	              )
	            {
					pa->SetRGB(newc);
	            	a->Update(p);
	            	q->AppendL(TPoint(x, y-1));
	            	q->AppendL(TPoint(x+1, y-1));
	            	q->AppendL(TPoint(x+1, y));
	            	q->AppendL(TPoint(x+1, y+1));
	            	q->AppendL(TPoint(x, y+1));
	            	q->AppendL(TPoint(x-1, y+1));
	            	q->AppendL(TPoint(x-1, y));
	            	q->AppendL(TPoint(x-1, y-1));
	            }
	        }		   
		    CleanupStack::PopAndDestroy(pa);
		    delete q;
	        return a;
	}

CArea* CImageUtils::FillEdgeAreaL(CFbsBitmap *aImage, TPoint aBegin, CArea *face)
	{
	CFbsBitmap* img = CopyImageL(aImage);
	CleanupStack::PushL(img);
	TBitmapUtil util(img);
	TSize s = aImage->SizeInPixels();
    CArea *a = CArea::NewL(aBegin, s.iHeight);

    TUint32 newc = TRgb(125, 125, 125).Value();
    TUint32 black = TRgb(0, 0, 0).Value();

    CArrayFixFlat<TPoint> *q = new CArrayFixFlat<TPoint>(300);
    q->AppendL(aBegin);

    util.Begin(aBegin);
    while(q->Count() != 0)
    {
		TPoint p = q->At(0); 
    	q->Delete(0);
    	TInt x = p.iX;
    	TInt y = p.iY;

        if(y<face->GetTop().iY || y>face->GetBottom().iY || x<face->GetLeftBorderL(y).iX || x > face->GetRightBorderL(y).iX)
            continue;

        util.SetPos(p);
        TRgb c = util.GetPixel();

        if(
        	  c.Value() != black  &&
        	  c.Value() != newc
          )
        {
			util.SetPixel(newc);
        	a->Update(p);
        	q->AppendL(TPoint(x, y-1));
        	q->AppendL(TPoint(x+1, y-1));
        	q->AppendL(TPoint(x+1, y));
        	q->AppendL(TPoint(x+1, y+1));
        	q->AppendL(TPoint(x, y+1));
        	q->AppendL(TPoint(x-1, y+1));
        	q->AppendL(TPoint(x-1, y));
        	q->AppendL(TPoint(x-1, y-1));
        }
    }
    util.End();
    CleanupStack::PopAndDestroy(img);
    delete q;
    return a;
	}

CArea* CImageUtils::FillEdgeAreaCVL(CNokiaCVImage *aImage, TPoint aBegin, CArea *face)
	{
	CPixelAccess *pa = CPixelAccess::NewLC(aImage);
	CleanupStack::PushL(pa);
	TSize s = aImage->Size();
	CArea *a = CArea::NewL(aBegin, s.iHeight);

    TUint32 newc = TRgb(125, 125, 125).Value();
    TUint32 black = TRgb(0, 0, 0).Value();

    CArrayFixFlat<TPoint> *q = new CArrayFixFlat<TPoint>(300);
    q->AppendL(aBegin);

    while(q->Count() != 0)
    {
		TPoint p = q->At(0); 
    	q->Delete(0);
    	TInt x = p.iX;
    	TInt y = p.iY;

        if(y<face->GetTop().iY || y>face->GetBottom().iY || x<face->GetLeftBorderL(y).iX || x > face->GetRightBorderL(y).iX)
            continue;

        pa->SetPos(p);
        TRgb c;
        aImage->GetPixel(c, p);

        if(
        	  c.Value() != black  &&
        	  c.Value() != newc
          )
        {
			pa->SetPixel(newc);
        	a->Update(p);
        	q->AppendL(TPoint(x, y-1));
        	q->AppendL(TPoint(x+1, y-1));
        	q->AppendL(TPoint(x+1, y));
        	q->AppendL(TPoint(x+1, y+1));
        	q->AppendL(TPoint(x, y+1));
        	q->AppendL(TPoint(x-1, y+1));
        	q->AppendL(TPoint(x-1, y));
        	q->AppendL(TPoint(x-1, y-1));
        }
    }
    CleanupStack::PopAndDestroy(pa);
    delete q;
    return a;
	}

CFbsBitmap* CImageUtils::DoSobel(CNokiaCVImage *aImage, CArea *face)
	{
		RMask mask1;
		RMask mask2;
		mask1.Open(1);
		mask2.Open(2);
		
		TInt i, j, k, l;
		TInt posX, posY;
		TInt sumRedX = 0;
		TInt sumRedY = 0;
		TInt sumGreenX = 0;
		TInt sumGreenY = 0;
		TInt sumBlueX = 0;
		TInt sumBlueY = 0;
		TRgb color;
		TSize bitmapSize = aImage->Size();
		
		CFbsBitmap *result =  new (ELeave) CFbsBitmap();
		CleanupStack::PushL(result);
		result->Create(bitmapSize, EColor16M);
		TBitmapUtil resultUtil(result);
		
		resultUtil.Begin(TPoint(0,0)); 
		
		for(i=Max(face->GetTop().iY, 0); i<Min(face->GetBottom().iY, bitmapSize.iHeight-1); i++)  //pre riadky v oblasti tvare
		{
			for(j=Max(face->GetLeft().iX, 0); j<Min(face->GetRight().iX, bitmapSize.iWidth-1);j++) // pre stlpce v oblasti tvare
			{
					for(k=-1; k<=1; k++) //offset v riadku
					{
						for(l=-1; l<=1; l++)
						{
							posX = j-k;
							posY = i-l;
							
							//osetrenie, aby sme nesiahali na pixeli mimo obrazku
							if(posX < 0)
								posX = 0;
							if(posY < 0)
								posY = 0;
							if(posX > bitmapSize.iWidth)
								posX = bitmapSize.iWidth - 1;
							if(posY > bitmapSize.iHeight)
								posY = bitmapSize.iHeight - 1;
														
							TPoint p = TPoint(posX,posY);
							aImage->GetPixel(color, p);
														
							sumRedX = sumRedX + mask1.get(k+1,l+1)* color.Red();
							sumRedY = sumRedY + mask2.get(k+1,l+1)* color.Red();
							sumGreenX = sumGreenX + mask1.get(k+1,l+1)* color.Green();
							sumGreenY = sumGreenY + mask2.get(k+1,l+1)* color.Green();
							sumBlueX = sumBlueX + mask1.get(k+1,l+1)* color.Blue();
							sumBlueY = sumBlueY + mask2.get(k+1,l+1)* color.Blue();
						}
					}
					
					//vypocitane pre dany pixel
					resultUtil.SetPos(TPoint(j,i));
					//bitmapUtil.SetPos(TPoint(j, i));
					TInt red = Abs(sumRedX) + Abs(sumRedY);
					TInt green = Abs(sumGreenX) + Abs(sumGreenY);
					TInt blue = Abs(sumBlueX) + Abs(sumBlueY);
								
					//kontrola hranic
		            if(red > 255) red = 255;
		            if(green > 255) green = 255;
		            if(blue > 255) blue = 255;

					
					resultUtil.SetPixel(TRgb(red, green, blue).Value());
					sumRedX = 0;
					sumRedY = 0;
					sumGreenX = 0;
					sumGreenY = 0;
					sumBlueX = 0;
					sumBlueY = 0;
			}
		}
		mask1.Close();
		mask2.Close();
		resultUtil.End();
		CleanupStack::Pop(result);
		return result;
	}

CNokiaCVImage* CImageUtils::DoOwnSobelCV(CNokiaCVImage *aImage, CArea *face)
	{
		RMask mask1;
		RMask mask2;
		mask1.Open(1);
		mask2.Open(2);
		
		TInt i, j, k, l;
		TInt posX, posY;
		TInt sumRedX = 0;
		TInt sumRedY = 0;
		TInt sumGreenX = 0;
		TInt sumGreenY = 0;
		TInt sumBlueX = 0;
		TInt sumBlueY = 0;
		TRgb color;
		TSize bitmapSize = aImage->Size();
		
		CNokiaCVImage *result = CNokiaCVImage::NewL();
		CleanupStack::PushL(result);
		result->CreateL(bitmapSize, aImage->DisplayMode());
		CPixelAccess *pa = CPixelAccess::NewLC(result);
		
		for(i=Max(face->GetTop().iY, 0); i<Min(face->GetBottom().iY, bitmapSize.iHeight-1); i++)  //pre riadky v oblasti tvare
		{
			for(j=Max(face->GetLeft().iX, 0); j<Min(face->GetRight().iX, bitmapSize.iWidth-1);j++) // pre stlpce v oblasti tvare
			{
					for(k=-1; k<=1; k++) //offset v riadku
					{
						for(l=-1; l<=1; l++)
						{
							posX = j-k;
							posY = i-l;
							
							//osetrenie, aby sme nesiahali na pixeli mimo obrazku
							if(posX < 0)
								posX = 0;
							if(posY < 0)
								posY = 0;
							if(posX > bitmapSize.iWidth)
								posX = bitmapSize.iWidth - 1;
							if(posY > bitmapSize.iHeight)
								posY = bitmapSize.iHeight - 1;
														
							TPoint p = TPoint(posX,posY);
							aImage->GetPixel(color, p);
														
							sumRedX = sumRedX + mask1.get(k+1,l+1)* color.Red();
							sumRedY = sumRedY + mask2.get(k+1,l+1)* color.Red();
							sumGreenX = sumGreenX + mask1.get(k+1,l+1)* color.Green();
							sumGreenY = sumGreenY + mask2.get(k+1,l+1)* color.Green();
							sumBlueX = sumBlueX + mask1.get(k+1,l+1)* color.Blue();
							sumBlueY = sumBlueY + mask2.get(k+1,l+1)* color.Blue();
						}
					}
					
					//vypocitane pre dany pixel
					pa->SetPos(TPoint(j,i));
					TInt red = Abs(sumRedX) + Abs(sumRedY);
					TInt green = Abs(sumGreenX) + Abs(sumGreenY);
					TInt blue = Abs(sumBlueX) + Abs(sumBlueY);
								
					//kontrola hranic
		            if(red > 255) red = 255;
		            if(green > 255) green = 255;
		            if(blue > 255) blue = 255;

					pa->SetRGB(TRgb(red, green, blue).Value());
					sumRedX = 0;
					sumRedY = 0;
					sumGreenX = 0;
					sumGreenY = 0;
					sumBlueX = 0;
					sumBlueY = 0;
			}
		}
		mask1.Close();
		mask2.Close();
		CleanupStack::PopAndDestroy(pa);
		CleanupStack::Pop(result);
		return result;
	}

CNokiaCVImage* CImageUtils::DoSobelCV(CNokiaCVImage *aImage)
	{
		CEdgeDetectionOp op(aImage, CEdgeDetectionOp::ESobel);
		CNokiaCVImage *result = CNokiaCVImage::NewL();
		result->CreateL(aImage->Size(), aImage->DisplayMode());		
		op.DoOperation(result);
				
		return result;
	}

CNokiaCVImage* CImageUtils::DoMedianCV(CNokiaCVImage *aImage)
	{
		CMedianFilterOp op(aImage, 3);
		CNokiaCVImage *result = CNokiaCVImage::NewL();
		result->CreateL(aImage->Size(), aImage->DisplayMode());	
		op.DoOperation(result);
		
		return result;
	}

void CImageUtils::ClearImage(CNokiaCVImage *aImage, TInt aLevel, CArea *aFace)//, TInt aNosePos)
	{
		CPixelAccess *pa = CPixelAccess::NewLC(aImage);
		TUint32 black = TRgb(0, 0, 0).Value();
		TUint32 white = TRgb(255, 255, 255).Value();
		TSize size = aImage->Size();
		for(TInt i=Max(aFace->GetTop().iY, 0); i<Min(aFace->GetBottom().iY, size.iHeight-1); i++)  //pre riadky v oblasti tvare
		{
			for(TInt j=Max(aFace->GetLeft().iX, 0); j<Min(aFace->GetRight().iX, size.iWidth-1);j++) // pre stlpce v oblasti tvare
			{
				TPoint p = TPoint(j, i);
				pa->SetPos(p);
				TRgb actual;
			    aImage->GetPixel(actual, p);
				
				if (actual.Red() < aLevel || actual.Green() < aLevel || actual.Blue() < aLevel)
					pa->SetPixel(black);
			    else
			    	pa->SetPixel(white);
			}
		}
		CleanupStack::Pop(pa);
		delete pa;
	}

TReal CImageUtils::GetPointDistance(TPoint aPoint1, TPoint aPoint2)
	{
		TReal result = 0.0;
		Math::Sqrt(result, Abs(aPoint2.iX - aPoint1.iX)*Abs(aPoint2.iX - aPoint1.iX) + Abs(aPoint2.iY - aPoint1.iY)*Abs(aPoint2.iY - aPoint1.iY));
		return result;
	}

//help functions
void CImageUtils::ShowFaceColor(CNokiaCVImage *aImage)
	{
		CPixelAccess *pa = CPixelAccess::NewLC(aImage);
		TSize size = aImage->Size();
		
		TUint32 black = TRgb(0, 0, 0).Value();
		TUint32 white = TRgb(255, 255, 255).Value();
		
		for(TInt row = 0; row < size.iHeight; row++)
		   for(TInt col = 0; col < size.iWidth; col++)
		   {
			   TRgb c;
			   TPoint p = TPoint(col, row);
			   aImage->GetPixel(c, p);
			   pa->SetPos(p);
			   
			   if(CImageUtils::IsFaceColorRGB(c))
				   pa->SetPixel(white);
			   else
				   pa->SetPixel(black);
			   
			  // RDebug::Print(_L("Spracovane - riadok:%d stlpec:%d"), row, col);
           }
		
		CleanupStack::PopAndDestroy(pa);
	}
